// pages/community/community.js
var colOneHeight = 0; // 第一列高度
var colTwoHeight = 0; // 第二列高度

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabSelected: '0',
    category: [{
      menuName: '推荐'
    }, {
      menuName: '附近'
    }, {
      menuName: '活动'
    }, {
      menuName: '关注'
    }],
    activitys: [{
      id: '1',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg1.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '看不见的西瓜',
      categoryType: "美食",
      count: 123,
      isZan: false
    }, {
      id: '2',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg2.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '美丽诱惑',
      categoryType: "健康",
      count: 74,
      isZan: true
    }, {
      id: '3',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg3.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '甜蜜美味',
      categoryType: "健康",
      count: 834,
      isZan: false
    }, {
      id: '4',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg4.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '有个梦想',
      categoryType: "健康",
      count: 234,
      isZan: false
    }, {
      id: '5',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg5.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '青春不记事',
      categoryType: "健康",
      count: 210,
      isZan: false
    }, {
      id: '6',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg6.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '在路上',
      categoryType: "健康",
      count: 173,
      isZan: false
    }, {
      id: '7',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg7.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '美丽回忆',
      categoryType: "健康",
      count: 104,
      isZan: false
    }, {
      id: '8',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg8.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '小东西',
      categoryType: "健康",
      count: 201,
      isZan: true
    }, {
      id: '9',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg9.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '好吃点',
      categoryType: "健康",
      count: 149,
      isZan: false
    }, {
      id: '10',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg11.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '美丽生活',
      categoryType: "健康",
      count: 95,
      isZan: false
    }, {
      id: '11',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg15.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '折梅又返',
      categoryType: "健康",
      count: 105,
      isZan: false
    }, {
      id: '12',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg16.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '轻舟向北',
      categoryType: "健康",
      count: 97,
      isZan: false
    }, {
      id: '13',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg17.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '轻舟向北',
      categoryType: "健康",
      count: 168,
      isZan: false
    }, {
      id: '14',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg18.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '细语凝香',
      categoryType: "健康",
      count: 199,
      isZan: false
    }, {
      id: '15',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg19.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '诗酒与素琴',
      categoryType: "健康",
      count: 366,
      isZan: false
    }, {
      id: '16',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg19.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '清酒与歌',
      categoryType: "健康",
      count: 488,
      isZan: false
    }, {
      id: '17',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg21.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '顶西瓜的小女孩',
      categoryType: "健康",
      count: 502,
      isZan: false
    }, {
      id: '18',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg22.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: 'argenteuil',
      categoryType: "健康",
      count: 63,
      isZan: false
    }, {
      id: '19',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg23.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: 'Millie',
      categoryType: "健康",
      count: 72,
      isZan: false
    }, {
      id: '20',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg24.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '雨后的爱情',
      categoryType: "健康",
      count: 295,
      isZan: false
    }, {
      id: '21',
      imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/communityImg25.png',
      logo: "https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/userLogoImg.png",
      nickName: '迷失的猫',
      categoryType: "健康",
      count: 947,
      isZan: false
    }],
    activitys1: [], // 左边一列的数据
    activitys2: [], // 右边一列的数据
    imagesCount: 0 // 所有的商品的总数量
  },

  /**
   * 图片加载函数
   * 图片加载完成后，根据图片的高度，依次往第一列和第二列中推入数据
   */
  onImageLoad: function (options) {
    // console.log(options)
    let currentId = options.currentTarget.id;
    let imageWidth = options.detail.width; // 图片宽度
    let imageHeight = options.detail.height; // 图片高度

    let activitys = this.data.activitys; // 获取所有的数据资源
    let activityObj = null;

    for (let i = 0; i < activitys.length; i++) {
      if (activitys[i].id === currentId) {
        activityObj = activitys[i];
        break;
      }
    }

    let imagesCount = this.data.imagesCount - 1;
    let activitys1 = this.data.activitys1;
    let activitys2 = this.data.activitys2;

    if (colOneHeight <= colTwoHeight) {
      colOneHeight += imageHeight;
      activitys1.push(activityObj)
    } else {
      colTwoHeight += imageHeight;
      activitys2.push(activityObj);
    }

    let data = {
      imagesCount: imagesCount,
      activitys1: activitys1,
      activitys2: activitys2
    }

    if (!imagesCount) {
      data.activitys = [];
    }

    this.setData(data);
  },

  /**
   * 类目点击事件
   * 切换类目，搜索对应类目下的数据，从新渲染页面，更新瀑布流
   */
  clickCategory: function (e) {
    this.setData({
      tabSelected: e.currentTarget.dataset.idx
    })
  },
  // 发布评论跳转
  addComment: function () {
    wx.navigateTo({
      url: '../addComment/addComment'
    })
  },
  // 前往消息页面
  toMyMessage: function () {
    wx.navigateTo({
      url: '../information/information'
    })
  },
  // 前往搜索页
  toSearch: function () {
    wx.navigateTo({
      url: '../search/search'
    })
  },
  // 前往活动详情
  toActivityDetails: function () {
    wx.navigateTo({
      url: '../activity/activity'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.imagesCount = this.data.activitys.length;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let tempData = this.data.activitys;
    tempData = tempData.sort(function () {
      return 0.5 - Math.random()
    });
    this.setData({
      activitys: []
    })
    this.imagesCount = tempData.length;
    this.setData({
      activitys: tempData,
      activitys1: [],
      activitys2: []
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})